﻿<html>
<body>

<script type='text/javascript' src="Common.js"></script>
<script type='text/javascript' src="Configuration.js"></script>
<script type='text/javascript' src="Entities.js"></script>

<script type='text/javascript'>

function ClientTest()
{
	this.main = function()
	{
		ClientTest.ServerURL = 
			"http://"
			+ Configuration.ServerHostName
			+ ":"
			+ Configuration.ServerPortNumber;		

		ClientTest.changeMessageTextToDrinkCoke();
		ClientTest.changeMessageTextToDrinkPepsi();

		setInterval(this.displayCurrentMessageText, 1000);
	}

	ClientTest.changeMessageTextToDrinkCoke = function()
	{
		ClientTest.changeMessageTextAndWait
		(
			"Drink Coke!", 
			ClientTest.changeMessageTextToDrinkCoke
		);
	}

	ClientTest.changeMessageTextToDrinkPepsi = function()
	{
		ClientTest.changeMessageTextAndWait
		(
			"Drink Pepsi!", 
			ClientTest.changeMessageTextToDrinkPepsi
		);
	}

	ClientTest.changeMessageTextAndWait = function(messageText, functionToCallAfterWaiting)
	{
		NetworkHelper.httpGet
		(
			ClientTest.ServerURL 
				+ "/MessageBoard/1/?text=" 
				+ encodeURIComponent(messageText)
		);

		var millisecondsUntilNextChange = Math.floor
		(
			3000 + Math.random() * 7000 
		);

		setTimeout
		(
			functionToCallAfterWaiting, 
			millisecondsUntilNextChange
		);
	}

	this.displayCurrentMessageText = function()
	{
		var response = NetworkHelper.httpGet
		(
			ClientTest.ServerURL + "/MessageBoard/1"
		);		

		var responseAsXMLElement = XMLElement.parseFromString
		(
			response
		);

		var responseAsMessageBoard = MessageBoard.fromXMLElement
		(
			responseAsXMLElement
		);		

		document.write(responseAsMessageBoard.text + "<br />");
	}
}

new ClientTest().main();

</script>
</body>
</html>